<template>
  <div>
    <Pane>
      <span slot="title">个人中心</span>
      <section slot="content">
        <div class="personal-info">
          <ul>
            <li class="personal-info-item">
              <label for>管理员ID:</label>
              <span>{{userInfo.id}}</span>
            </li>
            <el-divider></el-divider>
            <li class="personal-info-item">
              <label for>帐号:</label>
              <span>{{userInfo.account}}</span>
            </li>
            <el-divider></el-divider>
            <li class="personal-info-item">
              <label for>用户组:</label>
              <span>{{userInfo.userGroup}}</span>
            </li>
            <el-divider></el-divider>
            <li class="personal-info-item">
              <label for>创建时间:</label>
              <span>{{userInfo.ctime}}</span>
            </li>
            <el-divider></el-divider>
            <li class="personal-info-item">
              <label for>管理员头像:</label>
              <el-upload
                class="avatar-uploader"
                action="http://localhost:5000/users/avatar_upload"
                :show-file-list="false"
                :on-success="handleAvatarSuccess"
                :before-upload="beforeAvatarUpload"
              >
                <img v-if="imageUrl" :src="imageUrl" class="avatar" />
                <i v-else class="el-icon-plus avatar-uploader-icon"></i>
              </el-upload>
            </li>
            <li>
              <el-button type="success" @click="handleSave">保存</el-button>
            </li>
          </ul>
        </div>
      </section>
    </Pane>
  </div>
</template>

<script>
import Pane from '@/components/Pane/Pane.vue'
// import { getUserInfo, updateAvatar, uploadAvatar } from '@/api/account.js'
import { getUserInfo, updateAvatar } from '@/api/account.js'
import moment from 'moment'

export default {
  name: 'Personal',
  props: {},
  data() {
    return {
      imageUrl: '',
      userInfo: {},
      imgBaseUrl: 'http://localhost:5000/upload/imgs/acc_img/'
    }
  },
  methods: {
    async init() {
      const res = await getUserInfo()
      this.userInfo = res
      this.userInfo.ctime = moment(this.userInfo.ctime).format('YYYY-MM-DD')
      this.imageUrl = this.imgBaseUrl + this.userInfo.imgUrl
      // console.log(res)
    },
    handleAvatarSuccess(res, file) {
      // console.log('图片上传成功')
      const { code, msg, imgUrl } = res
      // console.log(code, msg, imgUrl)
      if (code === 0) {
        this.$message.success(msg)
        this.imageUrl = this.imgBaseUrl + imgUrl
        // const data = await uploadAvatar({ file: file })
        // console.log(file.raw)
        // console.log(data)
      }
      // this.imageUrl = URL.createObjectURL(file.raw)
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg'
      const isLt2M = file.size / 1024 / 1024 < 2

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!')
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!')
      }
      return isJPG && isLt2M
    },
    async handleSave() {
      // http://localhost:5000/upload/imgs/acc_img/1596175499087.jpg
      const imgUrlArr = this.imageUrl.split('/')
      const imgUrl = imgUrlArr[imgUrlArr.length - 1]
      const { code, msg } = await updateAvatar({ imgUrl: imgUrl })
      if (code === 0) {
        this.$message.success(msg)
      }
      // console.log(imgUrl)
    }
  },
  components: {
    Pane
  },
  created() {
    this.init()
  }
}
</script>

<style scoped >
ul,
li {
  margin: 0;
  padding: 0;
}
li {
  list-style: none;
}
.personal-info-item span {
  margin-left: 20px;
}
.personal-info-item img {
  width: 100%;
  height: 100%;
}
.personal-info >>> .el-upload {
  width: 100px;
  height: 100px;
}
.personal-info >>> .avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 100px !important;
  height: 100px !important;
  line-height: 100px !important;
  text-align: center;
}
.personal-info >>> .avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.personal-info >>> .avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.personal-info >>> .avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.personal-info >>> .avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
